<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学期" prop="semeId">
        <el-select v-model="queryParams.semeId" placeholder="请选择学期" :style="{width: '100%'}" @change="getList()">
          <el-option v-for="(item, index) in semeOptions" :key="index" :label="item.value"
                     :value="item.key" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分党校" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择分党校" filterable  @change="getList()"
                   :style="{width: '100%'}" >
          <el-option v-for="(item, index) in deptOptions" :key="index" :label="item.value"
                     :value="item.key" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="downLoadCertificationExcelHandler">下载证书</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请ID" align="center" prop="applyId" />
      <el-table-column label="真实姓名" align="center" prop="realName"/>
      <el-table-column label="学号/工号" align="center" prop="hnusterId"/>
      <el-table-column label="成绩" align="center" prop="scores"/>
      <el-table-column label="总结" align="center" prop="summary"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { listApply, getApply, delApply, addApply, updateApply, applyPass } from "@/api/psedu-base/apply";
  import { semeAndPsDeptOption } from "@/api/psedu-base/selectOption"
  import { trainDataList } from "@/api/psedu-base/student";
  import {downloadCertificationExcel} from "../../../../api/psedu-base/student";

  export default {
    name: "trainScoreIndex",
    dicts: ['sys_user_sex', 'apply_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 培训报名表格数据
        applyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          realName: null,
          hnusterId: null,
          psUserId: null,
          semeId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        semeOptions: [],
        deptOptions: [],
      };
    },
    created() {
      this.getSemeAndPsDeptOption().then(res => {
        this.getList();
      });
    },
    methods: {
      getSemeAndPsDeptOption() {
        console.log('--------')
        return semeAndPsDeptOption().then(res => {
          this.deptOptions = res.data.deptList;
          this.semeOptions = res.data.semeList;
          this.queryParams.deptId = this.deptOptions[0].key
          this.queryParams.semeId = this.semeOptions[0].key
          return Promise.resolve();
        })
      },
      /** 查询培训报名列表 */
      getList() {
        this.loading = true;
        trainDataList(this.queryParams).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          applyId: null,
          realName: null,
          psUserId: null,
          semeId: null,
          partyData: null,
          sex: null,
          nation: null,
          nativePlace: null,
          eduBackground: null,
          classes: null,
          phone: null,
          email: null,
          qq: null,
          submitPaperDate: null,
          checked: null,
          scores: null,
          summary: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.applyId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加培训报名";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const applyId = row.applyId || this.ids
        getApply(applyId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改培训报名";
        });
      },
      downLoadCertificationExcelHandler() {
        downloadCertificationExcel(this.queryParams.semeId, this.queryParams.deptId);
      },
      /** 通过申请 */
      handleApplyPass(row) {
        applyPass(row.applyId).then(res => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.applyId != null) {
              updateApply(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addApply(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const applyIds = row.applyId || this.ids;
        this.$modal.confirm('是否确认删除培训报名编号为"' + applyIds + '"的数据项？').then(function() {
          return delApply(applyIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('psedu-base/apply/export', {
          ...this.queryParams
        }, `apply_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
